package {
	import com.realeyes.osmfplayer.events.ControlBarEvent;
	import com.realeyes.osmfplayer.managers.ConfigurationManager;
	import com.realeyes.osmfplayer.view.ControlBarContainer;

	import org.osmf.display.ScaleMode;
	import org.osmf.elements.ImageElement;
	import org.osmf.elements.ParallelElement;
	import org.osmf.elements.VideoElement;
	import org.osmf.layout.HorizontalAlign;
	import org.osmf.media.MediaElement;
	import org.osmf.media.URLResource;
	import org.osmf.utils.URL;

	/**
	 * Creates a video player based off of an external XML configuration
	 * file.
	 * 
	 * @author	RealEyes Media
	 * @version	0.1
	 */ 
	[SWF(width="700", height="600")]
	[IconFile("osmf.png")]
	public class AugPlayer extends REOPS
	{
		
		//TODO - need to figure out why 2 streams showing up in manager when we play back a vid - one of them is unamed or something..grrr	
		//TODO - evaluate if we can update the control bar system to a media element and integrate in the player and the layout management via LayoutUtils
		//--need to look into MediaFactory
		
		/////////////////////////////////////////////
		//  DECLARATIONS
		/////////////////////////////////////////////
		
		static public const CAPTIONING_PLUGIN_PATH:String = "org.osmf.captioning.CaptioningPluginInfo";
		
		private var _configManager:ConfigurationManager;
		
		private var _augMediaElement:MediaElement;
		
		/**
		 * configPath	(String) the path for the config file
		 * This property can be set through FlashVars.
		 * 
		 * @default		"assets/data/reosmf_config.xml"
		 */
		
		
		
		//public static const CONFIG_DEFAULT_PATH:String = "assets/data/reosmf_config.xml";
		//public static const CONFIG_DEFAULT_PATH:String = "assets/data/PlayerConfig_DynStreams.xml";
		
		
		//TESTING ONLY		
		//private static const REMOTE_PROGRESSIVE:String = "http://mediapm.edgesuite.net/strobe/content/test/AFaerysTale_sylviaApostol_640_500_short.flv";
		//private static const TEST_VOD_URL:String = "rtmp://127.0.0.1/vod/sample";
		//private static const TEST_PROGRESSIVE_URL:String = "assets/seeker.flv";
		
		//public static const CONFIG_DEFAULT_PATH:String = "assets/data/PlayerConfig_MediaElements.xml";
		
		//public static const CONFIG_DEFAULT_PATH:String = "assets/data/PlayerConfig_DynStreams.xml";
		//public static const CONFIG_DEFAULT_PATH:String = "assets/data/PlayerConfig_Progressive.xml";
		//public static const CONFIG_DEFAULT_PATH:String = "assets/data/PlayerConfig_Streaming.xml";
		
		public static const CONFIG_DEFAULT_PATH:String = "assets/data/aug_config.xml";
		
		private static const LOGO_PNG:String = "http://localhost/reops/samplePlayer/bin-debug/logo.png";
		
		//REOPS RELEASE VERSION
		public static const VERSION:Number = 0.1;
		
		/////////////////////////////////////////////
		//  CONSTRUCTOR
		/////////////////////////////////////////////
		
		public function AugPlayer()
		{
			trace( "AugPlayer - Contruct" );
			super();
		}
		
	
		
		/**
		 * Initialize the control bar with settings pulled from the config file. Loads
		 * in the skin for the control bar
		 * 
		 * @return	void
		 */
		override protected function _initControlBar():void
		{
			_controlBar = new ControlBarContainer( _mediaPlayerShell, _playerConfig.isLive );
			
			_controlBar.hasCaptions = _playerConfig.hasCaptions;
			
			_controlBar.loadExternal(_playerConfig.skinConfig.path, _playerConfig.skinConfig.getSkinElements() );	
			
			_controlBar.addEventListener( ControlBarEvent.HIDE_CLOSEDCAPTION, _onRemoveClosedCaption );
			_controlBar.addEventListener( ControlBarEvent.SHOW_CLOSEDCAPTION, _onDisplayClosedCaption );
			
			this.addChild( _controlBar );
		}
		
	
		
		
		/**
		 * Adds in the default media types. Currently that is only VideoElement
		 * 
		 * @return void
		 */
//		override protected function _initMediaFactory():void
//		{
//			_mediaFactory = new REMediaFactory( _netConnectionFactory );	
//		}
		
	
		
		override protected function _initPlayerComponents():void
		{
			trace("-- init sequence --");
			
			var u:URL = new URL("http://mediapm.edgesuite.net/strobe/content/test/AFaerysTale_sylviaApostol_640_500_short.flv");
			 
			// Contstruct and image element:
			var logo:ImageElement = new ImageElement(new URLResource(new URL(LOGO_PNG)));
			logo.smoothing = true;

//			// Construct a layout properties object for the logo:			
//			var logoLayout:LayoutMetadata = new LayoutMetadata();
//			logo.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, logoLayout);;
//			
//			// Set the image to take 30% of its parent's width and height: 
//			logoLayout.percentWidth = 30;
//			logoLayout.percentHeight = 30;
//			
//			// Position the image's bottom-right corner 20 pixels away from
//			// the container's bottom-right corner:
//			logoLayout.bottom = 20;
//			logoLayout.right = 20;
//			
//			// Set the image to scale, keeping aspect ratio:
//			logoLayout.scaleMode = ScaleMode.LETTERBOX;
//			
//			// Instruct the image to be moved to the right hand side, should
//			// any surplus horizontal space be available after scaling:
//			logoLayout.horizontalAlign = HorizontalAlign.RIGHT;
//			
			// Construct a video element:
			var video:MediaElement = new VideoElement(new URLResource(u));
			
			// If no layout properties have been set on an element when it gets
			// added to a parent, then the the parent will set the target to
			// scale letter-box mode, vertical alignment to center, horizontal
			// alignment to middle, and set width and height to 100% of the
			// parent. This is fine for the video element at hand, so no additional
			// layout properties are set for it.		
			
			// Construct a parallel element that holds both the video and the
			// logo still: 
			var parallel:ParallelElement = new ParallelElement();
			parallel.addChild(video);
			parallel.addChild(logo);
			
			
			
			_mediaElement = parallel; // Set up the media to be played 
			
			_initMediaPlayer(); // Set up the player
			
			_initMediaElement(); // Kick it off
			
			_initControlBar(); // Set up control
		}
		
		
		
		/////////////////////////////////////////////
		//  GETTER/SETTERS
		/////////////////////////////////////////////
		
		
		
		
		/////////////////////////////////////////////
		//  HANDLERS
		/////////////////////////////////////////////
		
		
	}
}